import request from "../util/request";

const delay = time => {
  return new Promise(resolve => {
    setTimeout(resolve, time);
  });
};
export default {
  namespace: "cards",
  state: {
    cardsList: [],
    statistic: {}
  },
  effects: {
    *queryList(_, { call, put }) {
      const listData = [
        {
          name: "umi",
          desc: "极快的类 Next.js 的 React 应用框架",
          url: "https://umijs.org",
          id: 1
        },
        {
          name: "antd",
          desc: "一个服务于企业级产品的设计体系",
          url: "https://ant.design/index-cn",
          id: 2
        },
        {
          name: "antd-pro",
          desc: "一个服务于企业级产品的设计体系",
          url: "https://ant.design/index-cn",
          id: 3
        }
      ];
      yield put({ type: "initList", payload: listData });
    },
    *getStatistic(_, { call, put }) {
      const data = {
        1: [
          { genre: "Sports", sold: 275 },
          { genre: "Strategy", sold: 1150 },
          { genre: "Action", sold: 120 },
          { genre: "Shooter", sold: 350 },
          { genre: "Other", sold: 150 }
        ]
      };
      yield put({ type: "updateStatistic", payload: data });
    }
  },
  reducers: {
    initList(state, { payload }) {
      const cardsList = [...payload];
      return {
        cardsList
      };
    },
    addOne(state, { payload }) {
      return {
        ...state,
        cardsList: state.cardsList.concat([{ ...payload, id: 3 }])
      };
    },
    updateStatistic(state, { payload }) {
      return {
        ...state,
        statistic: payload
      };
    }
  }
};
